from django.db import models

# Create your models here.
from django.db.models import Model


# 用户表
class User(models.Model):
    ON = 1
    OFF = 2
    STATUS_CHOICES = {
        (ON, '正常'),
        (OFF, '锁定')
    }

    class Meta:
        verbose_name_plural = '用户列表'

    user_name = models.CharField(max_length=30, unique=True, verbose_name="用户名")
    pwd = models.CharField(max_length=200, verbose_name="密码")
    status = models.IntegerField(verbose_name="状态", choices=STATUS_CHOICES, default=ON)
    phone = models.CharField(max_length=50, unique=True, verbose_name="手机号")

    def __str__(self):
        return self.user_name


# 收货地址
class Address(models.Model):
    # 一对多
    class Meta:
        verbose_name_plural = '收货地址'

    user = models.ForeignKey("User", on_delete=models.CASCADE, verbose_name="用户名")
    user_name = models.CharField(max_length=30, verbose_name="收件人")
    phone = models.CharField(max_length=50, unique=True, verbose_name="手机号")
    address = models.CharField(max_length=200, null=False, verbose_name="地址")
    is_default = models.IntegerField(default=1)

    def __str__(self):
        return self.address + "(" + self.user_name + ")收，联系电话：" + self.phone


# 商品表
class Goods(models.Model):
    K = "克"
    J = "斤"
    G = "张"
    H = "部"
    V = "个"
    UNIT_CHOICES = {
        (K, "克"),
        (J, "斤"),
        (H, "张"),
        (V, "部"),
        (G, "个")

    }

    class Meta:
        verbose_name_plural = '商品表'

    good_name = models.CharField(max_length=30, verbose_name="商品名称")
    ing_url = models.ImageField(upload_to='img', verbose_name="图片")
    price = models.IntegerField(verbose_name="价格")
    # 多对多关系
    intro = models.TextField(verbose_name="介绍")
    type = models.ManyToManyField("Type", null=True, verbose_name="类型")
    unit = models.CharField(choices=UNIT_CHOICES, default="克", max_length=10, verbose_name="单位")

    def __str__(self):
        return self.good_name

    # 商品分类


class Type(models.Model):
    class Meta:
        verbose_name_plural = '商品分类'

    type_name = models.CharField(max_length=30, unique=True, verbose_name="商品类别")
    title_img = models.ImageField(upload_to='img', verbose_name="图片")

    def __str__(self):
        return self.type_name


# 订单表
class Order(models.Model):
    WFK = 1
    YFK = 2
    YFH = 3
    YSH = 4
    STATUS_CHOICESS = {
        (WFK, '未付款'),
        (YFK, '已付款'),
        (YFH, '已发货'),
        (YSH, '已收货')
    }
    HUO = 1
    WX = 2
    ZFB = 3
    YIN = 4
    PAY_STATUS_CHOICES = {
        (HUO, '货到付款'),
        (WX, '支付宝'),
        (ZFB, '微信支付'),
        (YIN, '银行卡支付')
    }

    class Meta:
        verbose_name_plural = '订单表'

    user = models.ForeignKey("User", on_delete=models.CASCADE, verbose_name="用户名")
    order_no = models.CharField(max_length=30, unique=True, verbose_name="订单号")
    status = models.IntegerField(verbose_name="状态", choices=STATUS_CHOICESS)
    time = models.DateTimeField(auto_now_add=True, verbose_name="下单时间")
    user_name = models.CharField(max_length=30, null=True, verbose_name="联系人")
    phone = models.CharField(max_length=50, verbose_name="手机号")
    address = models.CharField(max_length=30, verbose_name="地址")
    total_price = models.IntegerField(verbose_name="总价")
    pay_status = models.IntegerField(verbose_name="支付方式", default=HUO, choices=PAY_STATUS_CHOICES)


# 订单详情表
class OrderDetail(models.Model):
    class Meta:
        verbose_name_plural = '订单详情表'

    good_name = models.CharField(max_length=30, verbose_name="订单名")
    price = models.FloatField(verbose_name="价格")
    num = models.IntegerField(verbose_name="数量")
    ing_url = models.ImageField(max_length=100, verbose_name="图片")
    total_price = models.FloatField(verbose_name="总价")
    order = models.ForeignKey("Order", on_delete=models.CASCADE, verbose_name="订单号")


# 购物车
class Cart(models.Model):
    class Meta:
        verbose_name_plural = '购物车'

    num = models.IntegerField(verbose_name="数量")
    goods = models.ManyToManyField(Goods, verbose_name="商品名称")
    user = models.ForeignKey("User", on_delete=models.CASCADE, verbose_name="用户名")
